//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.paw.authorize.shiro.controller;

import cn.hutool.core.util.StrUtil;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {

  private static final Logger log = LoggerFactory.getLogger(LoginController.class);

  public LoginController () {
  }

  @GetMapping("/logout")
  public String logout(){
    SecurityUtils.getSubject().logout();
    return "/login";
  }

  @RequestMapping(value = "/login",method = {RequestMethod.GET,RequestMethod.POST})
  public String doLogin (@RequestParam(required = false) String userName, @RequestParam(required = false) String password,
      @RequestParam(required = false) String easyCaptcha, HttpServletRequest request) {
    Object principal = SecurityUtils.getSubject().getPrincipal();
    if (principal != null) {
      return "index";
    }
    if (!StrUtil.isEmpty(userName) && !StrUtil.isEmpty(password)) {
      try {
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        SecurityUtils.getSubject().login(token);
        HashMap<String, String> map = new HashMap(16);
        map.put("access_token", "1111111111111111111");
        return "index";
      } catch (IncorrectCredentialsException var7) {
        log.info("密码错误 {}", var7.getMessage());
      } catch (UnknownAccountException var8) {
        log.info("账号不存在 {}", var8.getMessage());
      } catch (LockedAccountException var9) {
        log.info("账号被锁定 {}", var9.getMessage());
      } catch (ExcessiveAttemptsException var10) {
        log.info("操作频繁，请稍后再试 {}", var10.getMessage());
      } catch (Exception var11) {
        log.error("登录异常 {}", var11.getMessage(), var11);
      }
    }
    return "login";
  }

  @GetMapping("/home")
  public String home(){
    return "homePage";
  }

}
